<template>
  <div class="content" ref="content">
    <div class="table-default" v-if="tableData">
      <el-button type="primary" class="backBtn" @click="backHandler">返回</el-button>
      <!-- <el-button type="warning" class="backBtn" @click="approveHandler(route.query.id)" v-if="tableData.state != '已审核'" v-loading.fullscreen.lock="fullscreenLoading"
        ><Icon name="wrench-screwdriver" />审核</el-button
      > -->
      <el-button type="success" @click="editClick" v-if="tableData.state != '已审核'">保存修改</el-button>
      <div id="base" ref="base">
        <el-descriptions :column="4" border title="基本信息">
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="其他出库单号">{{ tableData.othernumbers }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="部门">{{ tableData.leveltype }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="出库类型">{{ tableData.incomingtype }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="单据状态">{{ tableData.state }}</el-descriptions-item>
          <!-- <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="出库时间">{{ tableData.deliverydate }}</el-descriptions-item> -->
        </el-descriptions>
      </div>
      <div id="details" ref="details">
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="其他出库单明细" name="details">
            <el-table :data="tableData.detailscheck_all" border>
              <el-table-column prop="fnumber" label="品号" width="130" />
              <el-table-column prop="fname" label="品名" width="250" />
              <!-- <el-table-column prop="quantity" label="出库数量" width="180" v-if="tableData.state != '已审核'">
                <template #default="scope">
                  <el-input-number v-model="scope.row.quantity" :min="1" />
                </template>
              </el-table-column> -->
              <el-table-column prop="quantity" label="出库数量" width="180" >
                <template #default="scope">
                  {{ Number(scope.row.quantity) }}
                </template>
              </el-table-column>
              <el-table-column prop="funit_name" label="单位" width="80" />
              <el-table-column prop="barcode" label="条码" width="200" />
              <el-table-column prop="fnumber_stash" label="仓库名称" width="130" />
              <el-table-column prop="fname_stash" label="仓库编码" width="130" />
              <el-table-column prop="repositoryname" label="库位名称" width="130" />
              <el-table-column prop="warehousecode" label="库位代码" width="130" />
              <!-- <el-table-column prop="caigoucreator_id" label="创建者" width="130" /> -->
            </el-table>
            <Paginate />
          </el-tab-pane>

          <!-- <el-tab-pane label="其他出库汇总" name="summary">
            <el-table :data="summaryData" border>
              <el-table-column prop="fnumber" label="品号" width="130" />
              <el-table-column prop="fname" label="品名" width="250" />
              <el-table-column prop="quantity" label="出库数量" width="180" v-if="tableData.state != '已审核'">
                <template #default="scope">
                  <el-input-number v-model="scope.row.quantity" :min="1" />
                </template>
              </el-table-column>
              <el-table-column prop="quantity" label="出库数量" width="180" v-if="tableData.state == '已审核'">
                {{ Number(scope.row.quantity) }}
              </el-table-column>
              <el-table-column prop="funit_name" label="单位" width="80" />
              <el-table-column prop="warehousecode" label="库位代码" width="130" />
              <el-table-column prop="fname_stash" label="库位名称" width="130" />
            </el-table>
            <Paginate />
          </el-tab-pane> -->
        </el-tabs>
      </div>
      <!-- </el-scrollbar> -->
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { useRouter, useRoute } from 'vue-router'
import detailTableVue from './detaiTable.vue'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import { delDetailRouter } from '/admin/utils/delDetailRouter'

const router = useRouter()
const route = useRoute()

const api = `intelligent/otheroutbound/${route.query.id}`
const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data.data)

const tabName = ref('')

console.log('请求的详情', tableData)

const summaryData = ref([])

// const state = ref(route.query.state)

// const getDetailsAll = () => {
//   const api2 = `intelligent/otheroutbound/${route.query.id}?detailsAll=true`
//   http.get(api2).then(r => {
//     const res = r.data.data
//     console.log('汇总数据：', res)
//     summaryData.value = res.data
//   })
// }

// getDetailsAll()

const fullscreenLoading = ref(false)

const approveHandler = id => {
  fullscreenLoading.value = true

  http
    .put(`/intelligent/interfacedata/${id}`)
    .then(r => {
      fullscreenLoading.value = false

      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
  editClick()
}

const writestateFormatter = ref({
  0: '回写失败',
  1: '回写成功',
})

const searchbox = ref(null)
const showRef = ref(null)
const flag = ref(false)

console.log('路由参数接收', route.query.id)

const backHandler = () => {
  delDetailRouter('其它出库详情', '/inventory/OtherOutbound')
}
const isMore = ref<boolean>(false)

const activeName = ref('details')
const base = ref(null)
const details = ref(null)
const content = ref()
const currentId = route.query.id

// 修改
const editClick = () => {
  let detailObj = tableData.value.map(item => {
    return {
      // id: item.id,
      quantity: item.quantity,
    }
  })
  console.log('详情修改数据：', detailObj)

  let AllObj = summaryData.value.map(item => {
    return {
      giveaway: item.giveaway,
      provisional_id: item.provisional_id,
      purchaseorder_id: item.purchaseorder_id,
      purchaseorderschedule_id: item.purchaseorderschedule_id,
      supplier_id: item.supplier_id,
      purchasewarehousing_id: item.purchasewarehousing_id,
      inspectionlisttable_id: item.inspectionlisttable_id,
    }
  })

  console.log('汇总修改数据：', AllObj)

  // updateDetailsAll(AllObj)
  updateDetail(detailObj)
}

const updateDetail = data => {
  http.put(`/intelligent/otheroutbound/10/${currentId}`, data).then(r => {
    console.log(r.data.data)
    let res = r.data.data
    if ((res.code = '1000')) {
      ElMessage({
        message: res.message,
        type: 'success',
      })
    } else {
      ElMessage({
        message: res.message,
        type: 'error',
      })
    }
  })
}
const updateDetailsAll = data => {
  http.put(`/purchasing/summaryupdatedata/${currentId}`, data).then(r => {
    console.log(r.data.data)
    let res = r.data.data

    if ((res.code = '1000')) {
      ElMessage({
        message: res.message,
        type: 'success',
      })
    } else {
      ElMessage({
        message: res.message,
        type: 'error',
      })
    }
  })
}

onMounted(() => {
  search()
  deleted(reset)
})
</script>

<style lang="css">
/* .content{
  border: 2px solid red;
 overflow:hidden;
} */
.backBtn {
  margin: 20px 10px;
}

h2 {
  font-weight: bold;
}
#base {
  margin-bottom: 30px;
  /* border: 2px solid green; */
}
#details {
  /* border: 2px solid pink; */
}

.tabs {
  padding: 10px 0 0;
  /* border: 2px solid #000; */
  z-index: 999;
  background-color: #fff;
  position: sticky;
  top: -15px;
}

.tagRow {
  display: inline-block;
  /* border: 1px solid #000; */
  margin: 20px 10px;
}

.tagRow ul {
  display: flex;
  border: 1px solid #eee;
}
.tagRow ul li {
  margin: 0 5px;
  /* border: 1px solid #eee; */
  padding: 5px 10px;
  /* border-radius: 5px; */
}
.active {
  background-color: #409eff;
  color: #fff;
}
.labelClassNAme {
  width: 130px;
}
.contentClassName {
  width: 170px;
}
</style>
